package cn.usersmsg.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.usersmsg.biz.IUserBiz;
import cn.usersmsg.biz.UserBizIpml;
import cn.usersmsg.model.User;

public class GetCookies extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		User user = (User)session.getAttribute("user");
		if (user==null) {
			Cookie[] cookies = req.getCookies();		
			if(cookies!=null){
		    	for(int i=0;i<cookies.length;i++){
		    		if("username".equals(cookies[i].getName())){
		    			String username = cookies[i].getValue();
		    			IUserBiz iub = new UserBizIpml();
		    			user = iub.getUser(username);
		    			iub.userLogin(username, user.getUser_pwd());//cookie记录登陆后  模拟登陆下 修改登陆时间
		    			break;
		    		}
		    	}
		    }
		}		
		session.setAttribute("user", user);
		req.getRequestDispatcher("index.jsp").forward(req, resp);
	}

}
